﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;

namespace HYElectricSetting
{
	public partial class CableTrayFittingAdvancedSet : System.Windows.Forms.Form
	{
		public CableTrayFittingAdvancedSet(Autodesk.Revit.DB.Document doc, CableTrayFittingSet dlgCableTrayFittingSet, VerticalCableTrayFittingEntity fittingEntity)
		{
			CableTrayFittingAdvancedSet ss4__this = this;
			this.InitializeComponent();
			this.doc = doc;
			this.dlgCableTrayFittingSet = dlgCableTrayFittingSet;
			this.path = dlgCableTrayFittingSet.path;
			this.root = dlgCableTrayFittingSet.root;
			this.fittings = RevitFilter.GetElementsOfType(doc, typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_CableTrayFitting).ToElements();
			this.cableTray = dlgCableTrayFittingSet.cbxCableTray.SelectedItem.ToString();
			List<Element> source = (from type in dlgCableTrayFittingSet.cableTrayTypes
			where type.Name == dlgCableTrayFittingSet.cbxCableTrayType.SelectedItem.ToString()
			select type).ToList<Element>();
			this.cableTrayType = (from ct in (from ele in source
			select ele as CableTrayType).ToList<CableTrayType>()
                                  where ct.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME).AsString() == ss4__this.cableTray
			select ct).First<CableTrayType>();
			this.fittingEntity = fittingEntity.Clone();
		}

		private void CableTrayFittingAdvancedSet_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.FillFitting();
			this.RegisterSelEvent();
		}

		private void RegisterSelEvent()
		{
			foreach (object obj in base.Controls)
			{
				System.Windows.Forms.Control control = (System.Windows.Forms.Control)obj;
				bool flag = control is GroupBox;
				if (flag)
				{
					foreach (object obj2 in control.Controls)
					{
						System.Windows.Forms.Control control2 = (System.Windows.Forms.Control)obj2;
						bool flag2 = control2 is Button;
						if (flag2)
						{
							bool flag3 = control2.Text == "选项...";
							if (flag3)
							{
								Button button = control2 as Button;
								button.Click += this.button_Click;
							}
						}
						bool flag4 = control2 is YJKComboBox;
						if (flag4)
						{
							YJKComboBox hycomboBox = control2 as YJKComboBox;
							hycomboBox.SelectedIndexChanged += this.cbx_SelectedIndexChanged;
						}
					}
				}
			}
		}

		private void button_Click(object sender, EventArgs e)
		{
			int category = 0;
			int subCategory = 0;
            bool flag = (int)this.cableTrayType.ShapeType == 1;
			if (flag)
			{
				category = 1;
			}
			else
			{
                bool flag2 = (int)this.cableTrayType.ShapeType == 2;
				if (flag2)
				{
					category = 2;
				}
			}
			YJKComboBox hycomboBox = null;
			Button button = sender as Button;
			string name = button.Name;
			string text = name;
			uint num = YArchitech.Revit.PrivateImplementationDetails.ComputeStringHash(text);
			if (num <= 1258337177u)
			{
				if (num <= 580815425u)
				{
					if (num != 80288291u)
					{
						if (num == 580815425u)
						{
							if (text == "btnVerticalUpCornerTee")
							{
								subCategory = 17;
								hycomboBox = this.cbxVerticalUpCornerTee;
							}
						}
					}
					else if (text == "btnVerticalUpSideTee")
					{
						subCategory = 16;
						hycomboBox = this.cbxVerticalUpSideTee;
					}
				}
				else if (num != 760428003u)
				{
					if (num != 1106030182u)
					{
						if (num == 1258337177u)
						{
							if (text == "btnVerticalDownTee")
							{
								subCategory = 12;
								hycomboBox = this.cbxVerticalDownTee;
							}
						}
					}
					else if (text == "btnVerticalUpTee")
					{
						subCategory = 15;
						hycomboBox = this.cbxVerticalUpTee;
					}
				}
				else if (text == "btnRightDownSideElbow")
				{
					subCategory = 9;
					hycomboBox = this.cbxRightDownSideElbow;
				}
			}
			else if (num <= 2863118288u)
			{
				if (num != 1660537590u)
				{
					if (num != 2573487408u)
					{
						if (num == 2863118288u)
						{
							if (text == "btnRightUpSideElbow")
							{
								subCategory = 11;
								hycomboBox = this.cbxRightUpSideElbow;
							}
						}
					}
					else if (text == "btnLeftDownSideElbow")
					{
						subCategory = 8;
						hycomboBox = this.cbxLeftDownSideElbow;
					}
				}
				else if (text == "btnVerticalDownCornerTee")
				{
					subCategory = 14;
					hycomboBox = this.cbxVerticalDownCornerTee;
				}
			}
			else if (num != 2939838944u)
			{
				if (num != 3352734791u)
				{
					if (num == 3881662511u)
					{
						if (text == "btnLeftUpSideElbow")
						{
							hycomboBox = this.cbxLeftUpSideElbow;
							subCategory = 10;
						}
					}
				}
				else if (text == "btnVerticalUpCornerCross")
				{
					hycomboBox = this.cbxVerticalUpCornerCross;
					subCategory = 18;
				}
			}
			else if (text == "btnVerticalDownSideTee")
			{
				subCategory = 13;
				hycomboBox = this.cbxVerticalDownSideTee;
			}
			List<string> list = new List<string>();
			bool flag3 = hycomboBox.Items.Count > 0;
			if (flag3)
			{
				foreach (object obj in hycomboBox.Items)
				{
					string text2 = obj.ToString();
					list.Add(text2.Split(new char[]
					{
						':'
					}).First<string>());
				}
			}
			FittingSelect fittingSelect = new FittingSelect(this.doc, "桥架配件", "CableTrayFitting", category, subCategory, list);
			DialogResult dialogResult = fittingSelect.ShowDialog();
			bool flag4 = dialogResult == DialogResult.OK;
			if (flag4)
			{
				bool flag5 = fittingSelect.family != null;
				if (flag5)
				{
					this.loadFittingIds.Add(fittingSelect.family.Id);
					this.fittings = RevitFilter.GetElementsOfType(this.doc, typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_CableTrayFitting).ToElements();
					this.FillFitting();
					FamilySymbol familySymbol = null;
					foreach (FamilySymbol familySymbol2 in fittingSelect.family.GetSymbols())
					{
						bool flag6 = familySymbol2 != null;
						if (flag6)
						{
							familySymbol = familySymbol2;
							break;
						}
					}
					hycomboBox.SelectedItem = fittingSelect.family.Name + ":" + familySymbol.Name;
				}
				else
				{
					string text3 = "";
					bool flag7 = hycomboBox.Items.Count > 0;
					if (flag7)
					{
						foreach (object obj2 in hycomboBox.Items)
						{
							bool flag8 = obj2.ToString().Contains(fittingSelect.familyName);
							if (flag8)
							{
								text3 = obj2.ToString();
								break;
							}
						}
					}
					bool flag9 = text3 != "";
					if (flag9)
					{
						hycomboBox.SelectedItem = text3;
					}
				}
			}
		}

		private void cbx_SelectedIndexChanged(object sender, EventArgs e)
		{
			YJKComboBox hycomboBox = sender as YJKComboBox;
			string name = hycomboBox.Name;
			string text = hycomboBox.SelectedItem.ToString();
			string text2 = name;
			uint num = YArchitech.Revit.PrivateImplementationDetails.ComputeStringHash(text2);
			if (num <= 1271810065u)
			{
				if (num <= 637707703u)
				{
					if (num != 478015497u)
					{
						if (num == 637707703u)
						{
							if (text2 == "cbxVerticalDownCornerTee")
							{
								this.fittingEntity.VerticalDownCornerTee = text;
							}
						}
					}
					else if (text2 == "cbxVerticalDownSideTee")
					{
						this.fittingEntity.VerticalDownSideTee = text;
					}
				}
				else if (num != 692445179u)
				{
					if (num != 848227390u)
					{
						if (num == 1271810065u)
						{
							if (text2 == "cbxLeftDownSideElbow")
							{
								this.fittingEntity.LeftDownSideElbow = text;
							}
						}
					}
					else if (text2 == "cbxVerticalUpSideTee")
					{
						this.fittingEntity.VerticalUpSideTee = text;
					}
				}
				else if (text2 == "cbxVerticalUpTee")
				{
					this.fittingEntity.VerticalUpTee = text;
				}
			}
			else if (num <= 2746078148u)
			{
				if (num != 1579529158u)
				{
					if (num != 1962033236u)
					{
						if (num == 2746078148u)
						{
							if (text2 == "cbxRightDownSideElbow")
							{
								this.fittingEntity.RightDownSideElbow = text;
							}
						}
					}
					else if (text2 == "cbxVerticalUpCornerTee")
					{
						this.fittingEntity.VerticalUpCornerTee = text;
					}
				}
				else if (text2 == "cbxVerticalUpCornerCross")
				{
					this.fittingEntity.VerticalUpCornerCross = text;
				}
			}
			else if (num != 2975076578u)
			{
				if (num != 4159753128u)
				{
					if (num == 4226332507u)
					{
						if (text2 == "cbxRightUpSideElbow")
						{
							this.fittingEntity.RightUpSideElbow = text;
						}
					}
				}
				else if (text2 == "cbxVerticalDownTee")
				{
					this.fittingEntity.VerticalDownTee = text;
				}
			}
			else if (text2 == "cbxLeftUpSideElbow")
			{
				this.fittingEntity.LeftUpSideElbow = text;
			}
			this.dlgCableTrayFittingSet.IsEdited = true;
		}

		private void FillFitting()
		{
			bool flag = false;
			int partType = 0;
            bool flag2 = (int)this.cableTrayType.ShapeType == 1;
			if (flag2)
			{
				bool flag3 = this.cableTray == "带配件的电缆桥架";
				if (flag3)
				{
					partType = 42;
					flag = true;
				}
			}
			else
			{
                bool flag4 = (int)this.cableTrayType.ShapeType == 2;
				if (flag4)
				{
					bool flag5 = this.cableTray == "带配件的电缆桥架";
					if (flag5)
					{
						partType = 50;
						flag = true;
					}
				}
			}
			bool flag6 = flag;
			if (flag6)
			{
				this.FillFittingFamilyType(partType, this.cableTrayType, VerticalCableTrayFittingType.LeftDownSideElbow, this.cbxLeftDownSideElbow);
				this.FillFittingFamilyType(partType, this.cableTrayType, VerticalCableTrayFittingType.RightDownSideElbow, this.cbxRightDownSideElbow);
				this.FillFittingFamilyType(partType, this.cableTrayType, VerticalCableTrayFittingType.LeftUpSideElbow, this.cbxLeftUpSideElbow);
				this.FillFittingFamilyType(partType, this.cableTrayType, VerticalCableTrayFittingType.RightUpSideElbow, this.cbxRightUpSideElbow);
				this.FillFittingFamilyType(partType, this.cableTrayType, VerticalCableTrayFittingType.VerticalUpTee, this.cbxVerticalUpTee);
				this.FillFittingFamilyType(partType, this.cableTrayType, VerticalCableTrayFittingType.VerticalDownTee, this.cbxVerticalDownTee);
				this.FillFittingFamilyType(partType, this.cableTrayType, VerticalCableTrayFittingType.VerticalUpSideTee, this.cbxVerticalUpSideTee);
				this.FillFittingFamilyType(partType, this.cableTrayType, VerticalCableTrayFittingType.VerticalDownSideTee, this.cbxVerticalDownSideTee);
				this.FillFittingFamilyType(partType, this.cableTrayType, VerticalCableTrayFittingType.VerticalUpCornerTee, this.cbxVerticalUpCornerTee);
				this.FillFittingFamilyType(partType, this.cableTrayType, VerticalCableTrayFittingType.VerticalDownCornerTee, this.cbxVerticalDownCornerTee);
				this.FillFittingFamilyType(partType, this.cableTrayType, VerticalCableTrayFittingType.VerticalUpCornerCross, this.cbxVerticalUpCornerCross);
			}
		}

		private void FillFittingFamilyType(int partType, CableTrayType ctt, VerticalCableTrayFittingType cft, YJKComboBox cbx)
		{
			cbx.Items.Clear();
			foreach (Element element in this.fittings)
			{
				FamilySymbol familySymbol = element as FamilySymbol;
				Family family = familySymbol.Family;
				bool flag = family.get_Parameter(BuiltInParameter.FAMILY_CONTENT_PART_TYPE).AsInteger() == partType;
				if (flag)
				{
					Document document = this.doc.EditFamily(family);
					List<ConnectorElement> list = (from ele in RevitFilter.GetElementsOfType(document, typeof(ConnectorElement)).ToElements()
					select ele as ConnectorElement).ToList<ConnectorElement>();
					bool flag2 = list.Count == 2;
					if (flag2)
					{
						bool flag3 = !this.IsHorizontalFitting(list);
						bool flag4 = flag3;
						if (flag4)
						{
							bool flag5 = this.IsUpFitting(list);
							if (flag5)
							{
								VerticalCableTrayFittingType verticalCableTrayFittingType = this.JudgeElbowType(list, true);
								bool flag6 = cft == verticalCableTrayFittingType;
								if (flag6)
								{
									cbx.Items.Add(family.Name + ":" + familySymbol.Name);
								}
							}
							else
							{
								VerticalCableTrayFittingType verticalCableTrayFittingType2 = this.JudgeElbowType(list, false);
								bool flag7 = cft == verticalCableTrayFittingType2;
								if (flag7)
								{
									cbx.Items.Add(family.Name + ":" + familySymbol.Name);
								}
							}
						}
					}
					else
					{
						bool flag8 = list.Count == 3;
						if (flag8)
						{
							bool flag3 = !this.IsHorizontalFitting(list);
							bool flag9 = flag3;
							if (flag9)
							{
								bool flag10 = this.IsUpFitting(list);
								if (flag10)
								{
									VerticalCableTrayFittingType verticalCableTrayFittingType3 = this.JudgeTeeCrossFittingType(list, true);
									bool flag11 = cft == verticalCableTrayFittingType3;
									if (flag11)
									{
										cbx.Items.Add(family.Name + ":" + familySymbol.Name);
									}
								}
								else
								{
									VerticalCableTrayFittingType verticalCableTrayFittingType4 = this.JudgeTeeCrossFittingType(list, false);
									bool flag12 = cft == verticalCableTrayFittingType4;
									if (flag12)
									{
										cbx.Items.Add(family.Name + ":" + familySymbol.Name);
									}
								}
							}
						}
						else
						{
							bool flag13 = list.Count == 4;
							if (flag13)
							{
								bool flag3 = !this.IsHorizontalFitting(list);
								bool flag14 = flag3;
								if (flag14)
								{
									bool flag15 = !this.IsUpFitting(list);
									if (flag15)
									{
										VerticalCableTrayFittingType verticalCableTrayFittingType5 = this.JudgeTeeCrossFittingType(list, false);
										bool flag16 = cft == verticalCableTrayFittingType5;
										if (flag16)
										{
											cbx.Items.Add(family.Name + ":" + familySymbol.Name);
										}
									}
								}
							}
						}
					}
				}
			}
			this.SetDefaultValue(ctt, cft, cbx);
		}

		private void SetDefaultValue(CableTrayType ctt, VerticalCableTrayFittingType cft, YJKComboBox cbx)
		{
			switch (cft)
			{
			case VerticalCableTrayFittingType.LeftDownSideElbow:
				this.SelectDefaultValue(cbx, this.fittingEntity.LeftDownSideElbow);
				break;
			case VerticalCableTrayFittingType.RightDownSideElbow:
				this.SelectDefaultValue(cbx, this.fittingEntity.RightDownSideElbow);
				break;
			case VerticalCableTrayFittingType.LeftUpSideElbow:
				this.SelectDefaultValue(cbx, this.fittingEntity.LeftUpSideElbow);
				break;
			case VerticalCableTrayFittingType.RightUpSideElbow:
				this.SelectDefaultValue(cbx, this.fittingEntity.RightUpSideElbow);
				break;
			case VerticalCableTrayFittingType.VerticalUpTee:
				this.SelectDefaultValue(cbx, this.fittingEntity.VerticalUpTee);
				break;
			case VerticalCableTrayFittingType.VerticalDownTee:
				this.SelectDefaultValue(cbx, this.fittingEntity.VerticalDownTee);
				break;
			case VerticalCableTrayFittingType.VerticalUpSideTee:
				this.SelectDefaultValue(cbx, this.fittingEntity.VerticalUpSideTee);
				break;
			case VerticalCableTrayFittingType.VerticalDownSideTee:
				this.SelectDefaultValue(cbx, this.fittingEntity.VerticalDownSideTee);
				break;
			case VerticalCableTrayFittingType.VerticalUpCornerTee:
				this.SelectDefaultValue(cbx, this.fittingEntity.VerticalUpCornerTee);
				break;
			case VerticalCableTrayFittingType.VerticalDownCornerTee:
				this.SelectDefaultValue(cbx, this.fittingEntity.VerticalDownCornerTee);
				break;
			case VerticalCableTrayFittingType.VerticalUpCornerCross:
				this.SelectDefaultValue(cbx, this.fittingEntity.VerticalUpCornerCross);
				break;
			}
		}

		private void SelectDefaultValue(YJKComboBox cbx, string sel)
		{
			bool flag = !string.IsNullOrEmpty(sel);
			if (flag)
			{
				cbx.SelectedItem = sel;
			}
		}

		private bool IsHorizontalFitting(List<ConnectorElement> connectorElements)
		{
			bool result = true;
			ConnectorElement connectorElement = connectorElements[0];
			foreach (ConnectorElement connectorElement2 in connectorElements)
			{
				bool flag = Math.Abs(connectorElement.Origin.Z - connectorElement2.Origin.Z) < 0.001;
				if (flag)
				{
					result = false;
					break;
				}
			}
			return result;
		}

		private bool IsUpFitting(IList<ConnectorElement> connectorElements)
		{
			bool result = false;
			foreach (ConnectorElement connectorElement in connectorElements)
			{
				XYZ basisZ = connectorElement.CoordinateSystem.BasisZ;
				bool flag = basisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0));
				if (flag)
				{
					result = true;
					break;
				}
				bool flag2 = basisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0));
				if (flag2)
				{
					result = false;
					break;
				}
			}
			return result;
		}

		private VerticalCableTrayFittingType JudgeElbowType(IList<ConnectorElement> connectorElements, bool isUp)
		{
			VerticalCableTrayFittingType result = VerticalCableTrayFittingType.Undefined;
			XYZ xyz = null;
			XYZ xyz2 = null;
			XYZ xyz3 = null;
			foreach (ConnectorElement connectorElement in connectorElements)
			{
				XYZ xyz4 = new XYZ(connectorElement.Origin.X, connectorElement.Origin.Y, 0.0);
				bool flag = connectorElement.CoordinateSystem.BasisZ.IsAlmostEqualTo(isUp ? new XYZ(0.0, 0.0, 1.0) : new XYZ(0.0, 0.0, -1.0));
				if (flag)
				{
					xyz = xyz4;
				}
				else
				{
					xyz2 = xyz4;
					xyz3 = connectorElement.CoordinateSystem.BasisZ;
				}
			}
			Line line = Line.CreateUnbound(xyz2, -xyz3);
			XYZ xyzpoint = line.Project(xyz).XYZPoint;
			XYZ xyz5 = (xyz - xyzpoint).Normalize();
			double num = xyz3.AngleOnPlaneTo(xyz5, XYZ.BasisZ);
			bool flag2 = Math.Abs(Math.PI*.5 - num) < 0.001;
			if (flag2)
			{
				if (isUp)
				{
					result = VerticalCableTrayFittingType.LeftDownSideElbow;
				}
				else
				{
					result = VerticalCableTrayFittingType.LeftUpSideElbow;
				}
			}
			else
			{
				bool flag3 = Math.Abs(4.71238898038469 - num) < 0.001;
				if (flag3)
				{
					if (isUp)
					{
						result = VerticalCableTrayFittingType.RightDownSideElbow;
					}
					else
					{
						result = VerticalCableTrayFittingType.RightUpSideElbow;
					}
				}
			}
			return result;
		}

		private VerticalCableTrayFittingType JudgeTeeCrossFittingType(IList<ConnectorElement> connectorElements, bool isUp)
		{
			VerticalCableTrayFittingType result = VerticalCableTrayFittingType.Undefined;
			ConnectorElement connectorElement = null;
			List<ConnectorElement> list = new List<ConnectorElement>();
			foreach (ConnectorElement connectorElement2 in connectorElements)
			{
				bool flag = connectorElement2.CoordinateSystem.BasisZ.IsAlmostEqualTo(isUp ? new XYZ(0.0, 0.0, 1.0) : new XYZ(0.0, 0.0, -1.0));
				if (flag)
				{
					connectorElement = connectorElement2;
				}
				else
				{
					list.Add(connectorElement2);
				}
			}
			bool flag2 = list.Count == 2;
			if (flag2)
			{
				XYZ basisX = connectorElement.CoordinateSystem.BasisX;
				XYZ basisY = connectorElement.CoordinateSystem.BasisY;
				XYZ basisZ = list[0].CoordinateSystem.BasisZ;
				bool flag3 = list[1].CoordinateSystem.BasisZ.IsAlmostEqualTo(basisZ) || list[1].CoordinateSystem.BasisZ.IsAlmostEqualTo(-basisZ);
				if (flag3)
				{
					bool flag4 = basisX.IsAlmostEqualTo(basisZ) || basisX.IsAlmostEqualTo(-basisZ);
					if (flag4)
					{
						if (isUp)
						{
							result = VerticalCableTrayFittingType.VerticalDownSideTee;
						}
						else
						{
							result = VerticalCableTrayFittingType.VerticalUpSideTee;
						}
					}
					else
					{
						bool flag5 = basisY.IsAlmostEqualTo(basisZ) || basisY.IsAlmostEqualTo(-basisZ);
						if (flag5)
						{
							if (isUp)
							{
								result = VerticalCableTrayFittingType.VerticalDownTee;
							}
							else
							{
								result = VerticalCableTrayFittingType.VerticalUpTee;
							}
						}
					}
				}
				else if (isUp)
				{
					result = VerticalCableTrayFittingType.VerticalDownCornerTee;
				}
				else
				{
					result = VerticalCableTrayFittingType.VerticalUpCornerTee;
				}
			}
			else
			{
				bool flag6 = list.Count == 3;
				if (flag6)
				{
					bool flag7 = !isUp;
					if (flag7)
					{
						result = VerticalCableTrayFittingType.VerticalUpCornerCross;
					}
				}
			}
			return result;
		}

		private Document doc = null;

		private CableTrayFittingSet dlgCableTrayFittingSet = null;

		private string path = "";

		private XElement root = null;

		private string cableTray = "";

		private CableTrayType cableTrayType = null;

		private IList<Element> fittings = null;

		public List<ElementId> loadFittingIds = new List<ElementId>();

		public VerticalCableTrayFittingEntity fittingEntity = new VerticalCableTrayFittingEntity();
	}
}
